<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Admin\Actions\Grid;

use App\Models\CreditTrade;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;

class TradingInfo extends LazyRenderable
{
    use LazyWidget;

    public function grid(): Grid
    {
        $id = $this->payload['id'];
        $type = $this->payload['type'];

        if ($type == 1) {
            $model = CreditTrade::with(['sellTrades', 'sellUser'])
                ->where('buy_order_id', $id);

            return $this->getBuyGrid($model);
        }

        $model = CreditTrade::with(['buyTrades', 'buyUser'])
            ->where('sell_order_id', $id);
        return $this->getSellGrid($model);
    }

    /**
     * @param $model
     */
    public function getBuyGrid($model): Grid
    {
        return Grid::make($model, function (Grid $grid) {
            $grid->disableCreateButton();
            $grid->disableFilterButton();
            $grid->disableRefreshButton();
            $grid->disableViewButton();
            $grid->disableRowSelector();
            $grid->column('sellTrades.type', '类型')->using([
                1 => '买入',
                2 => '卖出',
            ]);
            $grid->column('sellUser.name', '会员名');

            $grid->column('sellTrades.amount', '挂卖数');
            $grid->column('sellTrades.traded_quantity', '已交易数');
            $grid->column('sellTrades.price', '交易单价');
            $grid->column('sellTrades.status', '状态')->using([
                1 => '交易中',
                2 => '交易完成',
                3 => '取消交易',
            ]);
            $grid->column('sellTrades.created_at', '交易时间');
        });
    }

    /**
     * @param $model
     */
    public function getSellGrid($model): Grid
    {
        return Grid::make($model, function (Grid $grid) {
            $grid->disableCreateButton();
            $grid->disableFilterButton();
            $grid->disableRefreshButton();
            $grid->disableViewButton();
            $grid->disableRowSelector();

            $grid->column('buyTrades.type', '类型')->using([
                1 => '买入',
                2 => '卖出',
            ]);
            $grid->column('buyUser.name', '会员名');
            $grid->column('buyTrades.amount', '交易数量');
            $grid->column('buyTrades.price', '交易单价');
            $grid->column('buyTrades.status', '状态')->using([
                1 => '交易中',
                2 => '交易完成',
                3 => '取消交易',
            ]);
            $grid->column('buyTrades.created_at', '交易时间');
        });
    }
}
